<%@ page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" %>

<%@ include file="layout/includes.jsp" %>
<%@ page import="com.example.crud.model.*" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="org.hibernate.criterion.Criterion" %>
<%@ page import="org.hibernate.criterion.Restrictions" %>

<%
    try {
        Integer key = Integer.parseInt(request.getParameter("i"));
        String entityName = request.getParameter("e").substring(0, 1).toUpperCase() + request.getParameter("e").substring(1);
        String daoName = "com.example.crud.model." + entityName + "DAO";
        Class daoClass = Class.forName(daoName);

        List<Criterion> crits = new ArrayList<Criterion>();
        crits.add(Restrictions.eq("id.pickerId", key));

        Object dao = daoClass.newInstance();

<#include "../auxftl/propertyutils.ftl">
<#include "../auxftl/viewutils.ftl">
<#foreach entity in c2j.getPOJOIterator(cfg.classMappings)>
<#assign entityName = entity.shortName/>
<#if isPickerEntity(entityName)>
        if (dao instanceof ${entityName}DAO) {
            List<${entityName}> list = ((${entityName}DAO)dao).findByCriteria((Criterion[]) crits.toArray(new Criterion[crits.size()]));
            out.print(list.size() > 0 ? list.get(0).getId().getPickerDisplay() : "");
            return;
        }
</#if>
</#foreach>
        throw new Exception("The given entity was not resolved to be of any known picker entity type!");

    } catch (Exception e) {
        out.print(e.toString());
    }
%>
